﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace hello_accinv_New_
{
    public partial class PaymentForm : Form
    {
        public PaymentForm()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void txtboxTotalCash_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(Char.IsDigit(e.KeyChar) || e.KeyChar == '.' || (Keys)e.KeyChar == Keys.Back || (Keys)e.KeyChar == Keys.Enter);
            
            if ((Keys)e.KeyChar == Keys.Enter)
            {
                decimal totalPrice = Decimal.Parse(txtboxTotalPrice.Text);
                decimal totalCash = Decimal.Parse(txtboxTotalCash.Text);

                if (totalCash >= totalPrice)
                {
                    txtboxTotalCash.Text = totalCash.ToString("#,#0.00#");

                    decimal totalChanges = totalCash - totalPrice;

                    txtboxChanges.Text = totalChanges.ToString("#,#0.00#");

                    CalculateChanges(totalChanges);
                }
                else
                {
                    txtboxChanges.Clear();
                    txtboxC1000.Clear();
                    txtboxC1000.BackColor = Color.White;
                    txtboxC500.Clear();
                    txtboxC500.BackColor = Color.White;
                    txtboxC100.Clear();
                    txtboxC100.BackColor = Color.White;
                    txtboxC50.Clear();
                    txtboxC50.BackColor = Color.White;
                    txtboxC20.Clear();
                    txtboxC20.BackColor = Color.White;
                    txtboxC10.Clear();
                    txtboxC10.BackColor = Color.White;
                    txtboxC5.Clear();
                    txtboxC5.BackColor = Color.White;
                    txtboxC1.Clear();
                    txtboxC1.BackColor = Color.White;
                }
            }
        }

        private void CalculateChanges(decimal changes)
        {
            //1000
            txtboxC1000.Text = ((int)(changes / 1000)).ToString();
            changes = changes - (Int32.Parse(txtboxC1000.Text) * 1000);
            txtboxC1000.BackColor = (txtboxC1000.Text.Equals("0")) ? Color.White : Color.LightGreen; 
                
            //500
            txtboxC500.Text = ((int)(changes / 500)).ToString();
            changes = changes - (Int32.Parse(txtboxC500.Text) * 500);
            txtboxC500.BackColor = (txtboxC500.Text.Equals("0")) ? Color.White : Color.LightGreen; 
               
            //100
            txtboxC100.Text = ((int)(changes / 100)).ToString();
            changes = changes - (Int32.Parse(txtboxC100.Text) * 100);
            txtboxC100.BackColor = (txtboxC100.Text.Equals("0")) ? Color.White : Color.LightGreen; 
                
            //50
            txtboxC50.Text = ((int)(changes / 50)).ToString();
            changes = changes - (Int32.Parse(txtboxC50.Text) * 50);
            txtboxC50.BackColor = (txtboxC50.Text.Equals("0")) ? Color.White : Color.LightGreen;     

            //20
            txtboxC20.Text = ((int)(changes / 20)).ToString();
            changes = changes - (Int32.Parse(txtboxC20.Text) * 20);
            txtboxC20.BackColor = (txtboxC20.Text.Equals("0")) ? Color.White : Color.LightGreen;     

            //10
            txtboxC10.Text = ((int)(changes / 10)).ToString();
            changes = changes - (Int32.Parse(txtboxC10.Text) * 10);
            txtboxC10.BackColor = (txtboxC10.Text.Equals("0")) ? Color.White : Color.LightGreen;     

            //5
            txtboxC5.Text = ((int)(changes / 5)).ToString();
            changes = changes - (Int32.Parse(txtboxC5.Text) * 5);
            txtboxC5.BackColor = (txtboxC5.Text.Equals("0")) ? Color.White : Color.LightGreen;     

            //1
            txtboxC1.Text = ((int)changes).ToString();
            txtboxC1.BackColor = (txtboxC1.Text.Equals("0")) ? Color.White : Color.LightGreen; 
        }
    }
}
